//
//  YDAccount.m
//  IngCard
//
//  Created by kf on 2017/7/10.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDAccount.h"

#define Key_Account_LoginToken @"Key_Account_LoginToken"
#define Key_Account_Uid @"Key_Account_Uid"

@implementation YDAccountEducation
@end

@implementation YDAccountOccupation
@end

@implementation YDAccount
@synthesize login_token = _login_token;
@synthesize uid = _uid;

-(void) clear
{
    self.uid = nil;
    self.login_token = nil;
}

-(void) loadDB
{
    self.uid = NSStringMake([[NSUserDefaults standardUserDefaults] valueForKey:Key_Account_Uid]);
    self.login_token = NSStringMake([[NSUserDefaults standardUserDefaults] valueForKey:Key_Account_LoginToken]);
}


-(void) serializeWith:(NSDictionary *) dic
{
    self.uid = NSStringMake(dic[@"uid"]);
    self.mobile = NSStringMake(dic[@"mobile"]);
    self.head_pic = NSStringMake(dic[@"head_pic"]);
    self.email = NSStringMake(dic[@"email"]);
    self.firstName = NSStringMake(dic[@"first_name"]);
    self.lastName = NSStringMake(dic[@"last_name"]);
    self.name = NSStringMake(dic[@"real_username"]);
    self.company = NSStringMake(dic[@"company_name"]);
    self.occupation = NSStringMake(dic[@"occupation"]);
    self.public_profile_url = NSStringMake(dic[@"public_profile_url"]);
    self.location = NSStringMake(dic[@"location"]);
    
    NSMutableArray* occupations = [NSMutableArray array];
    NSArray *occupationsArray = NSArrayMake(dic[@"occupation_experience"]);
    for (int i = 0; i < occupationsArray.count; i++) {
        NSDictionary *occupationDic = NSDictionaryMake(occupationsArray[i]);
        
        YDAccountOccupation *occupation = [[YDAccountOccupation alloc] init];
        occupation.ID = NSStringMake(occupationDic[@"id"]);
        occupation.title = NSStringMake(occupationDic[@"title"]);
        occupation.companyId = NSStringMake(occupationDic[@"company_id"]);
        occupation.companyName = NSStringMake(occupationDic[@"company_name"]);
        occupation.location = NSStringMake(occupationDic[@"location"]);
        occupation.startYear = NSStringMake(occupationDic[@"start_year"]);
        occupation.startMonth = NSStringMake(occupationDic[@"start_month"]);
        occupation.endYear = NSStringMake(occupationDic[@"end_year"]);
        occupation.endMonth = NSStringMake(occupationDic[@"end_month"]);
        occupation.occDescription = NSStringMake(occupationDic[@"description"]);
        occupation.updateTime = NSStringMake(occupationDic[@"update_time"]);
        
        [occupations safeAddObject:occupation];
    }
    self.occupations = occupations;
    
    NSMutableArray *educations = [NSMutableArray array];
    NSArray *educationsArray = NSArrayMake(dic[@"education_experience"]);
    for (int i = 0; i < educationsArray.count ; i++)
    {
        NSDictionary *educationDic = NSDictionaryMake(educationsArray[i]);
        
        YDAccountEducation *education = [[YDAccountEducation alloc] init];
        education.ID = NSStringMake(educationDic[@"id"]);
        education.schoolId = NSStringMake(educationDic[@"school_id"]);
        education.schoolName = NSStringMake(educationDic[@"school_name"]);
        education.degree = NSStringMake(educationDic[@"degree"]);
        education.major = NSStringMake(educationDic[@"major"]);
        education.startTime = NSStringMake(educationDic[@"start_time"]);
        education.endTime = NSStringMake(educationDic[@"end_time"]);
        education.eduDescription = NSStringMake(educationDic[@"description"]);
        education.updateTime = NSStringMake(educationDic[@"update_time"]);
        
        [educations safeAddObject:education];
    }
    self.educations = educations;
}

#pragma mark - Lazy loading
-(void) setLogin_token:(NSString *)login_token
{
    if (![login_token isEqualToString: _login_token])
    {
        _login_token = login_token;
        
        if (!login_token)
        {
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:Key_Account_LoginToken];
        }
        else
        {
            [[NSUserDefaults standardUserDefaults] setValue:login_token forKey:Key_Account_LoginToken];
        }
    }
}

-(void) setUid:(NSString *)uid
{
    if (![uid isEqualToString: _uid])
    {
        _uid = uid;
        
        if (!uid)
        {
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:Key_Account_Uid];
        }
        else
        {
            [[NSUserDefaults standardUserDefaults] setValue:uid forKey:Key_Account_Uid];
        }
    }
}
@end
